/**
 * 
 */
package com.yilin.medical.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * @title 
 * @company 北京扎根网络信息技术有限公司
 * @author Jack Zhang
 * @version 1.0
 * @created 2014-12-12 下午5:43:05
 * @changeRecord [修改记录]
 */
@SuppressLint("ViewConstructor")
public class JxScrollView extends ScrollView {
	
	// 滑动距离及坐标
    private float xDistance, yDistance, xLast, yLast;
    
    private final String TAG = HomeScrollView.class.getSimpleName();
    onScroll scroll;

	public JxScrollView(Context context) {
		super(context);
	}
	public JxScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	 public interface OnScrollChangedListener{
	      public void onScrollChanged(int x, int y, int oldxX, int oldY);
	  }
	  
	  private OnScrollChangedListener onScrollChangedListener;
	  
	  /**
	  * 
	  * @param onScrollChangedListener
	  */
	  public void setOnScrollListener(OnScrollChangedListener onScrollChangedListener){
	      this.onScrollChangedListener=onScrollChangedListener;
	  }
	  
	/* (non-Javadoc)
	 * @see android.view.View#onScrollChanged(int, int, int, int)
	 */
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);
		if(onScrollChangedListener != null){
			onScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
		}
	}
	public void setScroll(onScroll scroll) {
		this.scroll = scroll;
	}
	@Override
	protected void dispatchDraw(Canvas canvas) {
		if (scroll != null) {
			scroll.scrollY(getScrollY() / 2);
		}
		super.dispatchDraw(canvas);
	}
	public interface onScroll {
		public void scrollY(float dy);
	}
	@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                
                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;
                
                if(xDistance > yDistance){
                    return false;
                }  
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }
	
}
